#define _CRT_SECURE_NO_WARNINGS
#include"string.h"

namespace bit
{
	const size_t string::nops = -1;

	void string::insert(size_t pos, const char* s)
	{
		assert(pos < _size);
		size_t len = strlen(s);
		cout << len << endl;
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}

		size_t end = len + _size;
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			end--;
		}

		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = s[i];
		}
		_size += len;
	}

	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			for (size_t i = pos + len; i <= _size; i++)
			{
				_str[i - len] = _str[i];
			}
			_size -= len;
		}

	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}
	void  string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}

		strcpy(_str + _size, str);
		_size += len;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size;
		while (end >= pos)
		{
			_str[end + 1] = _str[end];
			--end;
		}
		_str[pos] = ch;
		++_size;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';

	}
	string string::substr(size_t pos, size_t len)
	{
		if (len >= _size - pos)
		{
			len = _size - pos;
		}

		string sub;
		sub.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			sub += _str[pos + i];
		}

		return sub;
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
				return i;
			return nops;
		}
	}

	size_t string::find(const char* str, size_t pos)
	{
		assert(pos < _size);
		const char* ptr = strstr(_str + pos, str);

		if (ptr == nullptr)
		{
			return nops;
		}
		else
		{
			return ptr - _str;
		}
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}


	void test_string()
	{
		string s1;
		string s2("hello world");
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s2.size(); i++)
		{
			s2[i] += 2;
		}
		cout << s2.c_str() << endl;

		for (auto e : s2)
		{
			cout << e << " ";
		}

		string::iterator it = s2.begin();
		while (it != s2.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void Test_string1()
	{
		//string s1("hello world");
		//s1 += 'X';
		//s1 += 'Y';
		//cout << s1.c_str() << endl;

		//s1 += "   electrict";
		//cout << s1.c_str() << endl;

		//s1.insert(4, '*');
		//cout << s1.c_str() << endl;


		string s2("hello world");
		cout << s2.c_str() << endl;
		s2.insert(5, "***");
		cout << s2.c_str() << endl;

	}

	void Test_string4()
	{
		string s("test.cpp.zip");
		size_t pos = s.find('.');
		//string suffix = s.substr(pos);
		//cout << suffix.c_str() << endl;

	}

	bool operator < (const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str());
	}
	bool  operator <= (const string& s1, const string& s2)
	{
		return s1 == s2 || s1 < s2;
	}
	bool  operator >(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}
	bool  operator >= (const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}
	bool  operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;

	}
	bool  operator != (const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

	ostream& operator <<(ostream& out, const string& s)
	{
		for (auto& ch : s)
		{
			out << ch;
		}
		return out;
	}
	istream& operator >>(istream& in, string& s)
	{
		char ch;
		//in >> ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			//in >> ch;
			ch = in.get();

		}
		return in;
	}


	void Test_string5()
	{
		string s1("hello  ");
		string s2("    world");
		//s1 = s1;
		//cout << s1.c_str() << endl;
		//string s3 = s1;
		//cout << s3.c_str() << endl;

		cout << s1 << s2 << endl;
		cin >> s1;
		cout << s1 << endl;

	}
	void Test_string3()
	{
		string s1("hello world");
		s1.erase(6, 100);
		cout << s1.c_str() << endl;

		string s2("hello world");
		s2.erase(6);
		cout << s2.c_str() << endl;

		string s3("hello world");
		s3.erase(6, 3);
		cout << s3.c_str() << endl;
	}


}